package com.cnap.multiclouldmgmt.vm.service;

import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.CommonResult;
import com.cnap.multiclouldmgmt.model.vmScale.ScaleGroupPolicy;
import com.cnap.multiclouldmgmt.repository.master.VMScaleMapper;
import com.cnap.multiclouldmgmt.vm.task.AsyncInvokeService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

import static com.cnap.multiclouldmgmt.vm.service.VMScale.currentThreadAndScalePolicyMap;
import static org.mockito.Mockito.doNothing;

public class VMScaleTest extends MulticlouldmgmtApplicationTests {

    @InjectMocks
    private VMScale vmScale;

    @Mock
    private VMScaleMapper vmScaleMapper;

    @Mock
    private AsyncInvokeService asyncInvokeService;

    @Test
    public void addScalePolicyTest() throws InterruptedException {

        List<ScaleGroupPolicy> vmScaleGroupPolicys =new ArrayList<>();
        ScaleGroupPolicy scaleGroupPolicy = new ScaleGroupPolicy();
        vmScaleGroupPolicys.add(scaleGroupPolicy);

        Mockito.when(vmScaleMapper.queryVmScaleGroupPolicyById(Mockito.any())).thenReturn(scaleGroupPolicy);
//        Mockito.when(userService.save(Mockito.any())).thenReturn(Boolean.TRUE);

        doNothing().when(asyncInvokeService).exec(Mockito.any());
        CommonResult<String> stringCommonResult = vmScale.addScalePolicy("10000");
        Assertions.assertEquals(stringCommonResult.getCode(),200);
    }

    @Test
    public void addScalePolicyTest1() throws InterruptedException {
        Mockito.when(vmScaleMapper.queryVmScaleGroupPolicyById(Mockito.any())).thenReturn(null);
        CommonResult<String> stringCommonResult = vmScale.addScalePolicy("10000");
        Assertions.assertEquals(stringCommonResult.getData(),"");
    }


    @Test
    public void updateScalePolicyTest() throws InterruptedException {
        currentThreadAndScalePolicyMap.put("1000",new Thread());
        List<ScaleGroupPolicy> vmScaleGroupPolicys =new ArrayList<>();
        ScaleGroupPolicy scaleGroupPolicy = new ScaleGroupPolicy();
        vmScaleGroupPolicys.add(scaleGroupPolicy);

        Mockito.when(vmScaleMapper.queryVmScaleGroupPolicyById(Mockito.any())).thenReturn(scaleGroupPolicy);
       // Mockito.when(currentThreadAndScalePolicyMap.save(Mockito.any())).thenReturn(Boolean.TRUE);

        doNothing().when(asyncInvokeService).exec(Mockito.any());
        CommonResult<String> stringCommonResult = vmScale.updateScalePolicy("1000");
        Assertions.assertEquals(stringCommonResult.getCode(),200);
    }

    @Test
    public void deleteScalePolicyTest() throws InterruptedException {
        currentThreadAndScalePolicyMap.put("1000",new Thread());
        doNothing().when(asyncInvokeService).exec(Mockito.any());
        CommonResult<String> stringCommonResult = vmScale.deleteScalePolicy("1000");
        Assertions.assertEquals(stringCommonResult.getCode(),200);
    }
}
